package com.mucas;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class mucas extends Activity implements OnItemClickListener {
	
	EditText in;
    ListView employeeList;
    Search filter;
    ListAdapter adapter;
    List<String> thisList;
    CourseStack cs;
    Connector dataSource;
    CourseExtractor ce;
    boolean generated;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        //EditText searchText;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        in 			 = (EditText) findViewById (R.id.searchText);
        employeeList = (ListView) findViewById(R.id.journalResults);
        this.filter  = new Search();
        cs       	 = new CourseStack();
        dataSource 	 = new Connector();
        ce   		 = new CourseExtractor();
        generated    = false;
        run();
        
    }
    
    public void run() {
        InputStream is		 = null;
        Document xcri		 = null;
        boolean init		 = true;
        
    	try {
			//dataSource.setURL("http://joshuamoon.co.uk/ULprogrammes_xcri.xml");
    		dataSource.setURL("http://lincoln.ac.uk/componants/xml/ULprogrammes_xcri.xml");
    			
		} catch (Exception e1) {
			init = false;
		}
        
        try {
        	is = dataSource.OpenHttpConnection();
        	
        } catch (Exception e2) {
        	init = false;
        }
        
        if(is instanceof InputStream && init == true) {
        	if(ce.setDataStream(is)) {
        	
	        	xcri = ce.getDoc();
	        	if(!generated) {
	        		cs.generateStack(xcri);
	        		generated = true;
	        	}
	        	
	        	thisList = new ArrayList<String>();
	        	
	        	for(int i = 0; i < cs.getStackSize(); i++) {
	        		
	        		if(filter.doFilter(cs.getCoursePos(i))) {
	        			thisList.add(cs.getCoursePos(i).toString());
	        		}
	        	}
	        	
	        	adapter      = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, thisList);
	            
	            employeeList.setAdapter(adapter);
	            employeeList.setOnItemClickListener(this);
        	}
        }
    }

    public void search(View view) {
    	filter.setPattern(this.in.getText().toString());
    	run();
    }

	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		Context context   = getApplicationContext();
    	long currPos 	  = adapter.getItemId(position);
    	Course currCourse = cs.findFromString(thisList.get((int) currPos));
    	int duration 	  = Toast.LENGTH_LONG;
    	CharSequence text = "";
    	if(currCourse != null)
    		text = currCourse.getDetails();
    	else
    		text = "Course not found error.";
    	Toast toast 	  = Toast.makeText(context, text, duration);
    	toast.show();
		
	}
    
    
}